﻿using BoriPeter.FitnessJournal8.DAL;
using BoriPeter.FitnessJournal8.Interfaces;
using BoriPeter.FitnessJournal8.Models;
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Search;
using Windows.System;
using Windows.UI.Xaml;

namespace BoriPeter.FitnessJournal8.ViewModels
{
    public abstract class ViewModelBase : Screen
    {
        #region Constructor
        public ViewModelBase(INavigationService _navigationService)
        {
            navigationService = _navigationService;
        }
        #endregion

        #region GoBack()
        public void GoBack()
        {
            OnGoBack();
            navigationService.GoBack();
        }

        protected virtual void OnGoBack()
        {
            /* This function shall be implemented in the derived classes! (Only if needed) */
        }

        public bool CanGoBack
        {
            get
            {
                return navigationService.CanGoBack;
            }
        }
        #endregion

        #region ProgressBar
        private bool progressBarIsIndeterminate = true;
        public bool ProgressBarIsIndeterminate
        {
            get
            {
                return progressBarIsIndeterminate;
            }
            set
            {
                progressBarIsIndeterminate = value;
                NotifyOfPropertyChange("ProgressBarIsIndeterminate");
                NotifyOfPropertyChange("ProgressBarVisibility");
            }
        }
        public Visibility ProgressBarVisibility
        {
            get
            {
                if (progressBarIsIndeterminate)
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
        }
        #endregion

        #region NutritionSource
        private static NutritionDataAbstraction nutritionSource;
        public static NutritionDataAbstraction NutritionSource
        {
            get 
            {
                if (nutritionSource == null)
                    nutritionSource = new NutritionDataAbstraction();
                return nutritionSource; 
            }
        }
        #endregion

        #region NavigationService
        private static INavigationService navigationService;
        public static INavigationService NavigationService
        {
            get { return navigationService; }
        }
        #endregion

        #region Navigate2FoodDetails()
        protected void Navigate2FoodDetails(Food _food)
        {
            NutritionSource.AddFood2History(_food);
            string id = _food.Id.ToString();
            navigationService.UriFor<FoodDetailsViewModel>()
                .WithParam(m => m.ParamId, id)
                .Navigate();
        }
        #endregion

        #region Navigate2AddNewEntry()
        protected void Navigate2AddNewEntry(Food _food)
        {
            string id = _food.Id.ToString();
            navigationService.UriFor<AddNewEntryViewModel>()
                .WithParam(m => m.ParamId, id)
                .Navigate();
        }
        #endregion

        #region Navigate2DailyEntry
        protected void Navigate2DailyEntry(DailyEntry _entry)
        {
            string date = _entry.Date.ToString();
            navigationService.UriFor<DailyEntryViewModel>()
                .WithParam(m => m.ParamDate, date)
                .Navigate();
        }
        #endregion

        #region OnKeyPressed
        public void OnKeyPressed(Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
        {
            if (((e.Key >= VirtualKey.A) && (e.Key <= VirtualKey.Z)) ||
                ((e.Key >= VirtualKey.Number0) && (e.Key <= VirtualKey.Number9)))
            {
                SearchPane.GetForCurrentView().Show(e.Key.ToString().Substring(e.Key.ToString().Length-1));
            }
        }
        #endregion

        private int itemHeight = 120;
        private int itemGap = 6;


        #region GridNumber
        private int gridNumber;
        public int GridNumber
        {
            get
            {
                int upGap = 300;
                int downGap = 50;
                var bounds = Window.Current.Bounds;
                int height = (int)bounds.Height;
                //double width = bounds.Width;
                this.gridNumber = (height - (upGap + downGap)) / (itemHeight + itemGap);
                return this.gridNumber;
            }

            set
            {
                this.gridNumber = value;
                NotifyOfPropertyChange("GridNumber");
            }
        }
        #endregion

        #region GridHeight
        private int gridHeight;
        public int GridHeight
        {
            get
            {
                var height = ((itemHeight + itemGap) * GridNumber)+2;

                return height;
            }

            set
            {
                this.gridHeight = value;
                NotifyOfPropertyChange("GridHeight");
            }
        }
        #endregion

    }
}
